package com.bkhech.home.practice.spring.factorybean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 自定义 FactoryBean 启动类
 *
 * @author guowm
 * @date 2023/1/29
 */
public class FactoryBeanApplicationDemo {
    public static void main(String[] args) throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanAFactoryBean.class);

        final BeanAFactoryBean beanAFactoryBean = context.getBean(BeanAFactoryBean.class);
        System.out.println(beanAFactoryBean);
        final BeanAFactoryBean beanAFactoryBean1 = context.getBean("&beanAFactoryBean", BeanAFactoryBean.class);
        System.out.println(beanAFactoryBean == beanAFactoryBean1);

        // 每次获取到的都是不同的 bean 实例
        final BeanA beanA = beanAFactoryBean.getObject();
        System.out.println(beanA);
        // 是否是相同的实例取决于 isSingleton()
        // 当时 FALSE 时，每次调用都会调用目标 getObject() 方法
        final BeanA beanA1 = context.getBean(BeanA.class);
        System.out.println(beanA1);

        System.out.println(beanA == beanA1);
    }
}
